//
//  AppDelegate.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/18.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
//1.0上架啦。。。。我是一个隐藏的bug
    var window: UIWindow?
    
    let key: String = "lastVersion"


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //加载自定义的Window
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.redColor()
        
        window?.rootViewController = loadRootViewController()
        
        window?.makeKeyAndVisible()
        //设置导航条Bar的肤色
        let navc = UINavigationBar.appearance()
        navc.tintColor = UIColor.orangeColor()

        return true
    }
    
    // MARK: - 之定义方法
    ///给外解设置rootViewController
    class func setupRootViewController(controller: UIViewController) {
        let window: UIWindow =  ((UIApplication.sharedApplication().delegate?.window)!)!
        window.rootViewController = controller
    }
    ///根据登录状态判断应该返回的控制器
    private func loadRootViewController() -> UIViewController{
        //是否登录
        let isLogin = KVUserAccountViewModel.sharedUserAccountViewModel.userIsLogin
        if isLogin {
            let newVersion = isNewVersion()
            if newVersion {
                return KVFeatureCVC()
            }
            return KVWelcomeVC()

        }
        //保存当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String

        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
        return KVBaseTabBarVC()
    }
    private func isNewVersion() -> Bool {
        //获取版本
           let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
            let lastVersion = ((NSUserDefaults.standardUserDefaults().stringForKey(key)))! as String
            let newVersion = currentVersion != lastVersion
        if newVersion {
            //保存新版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            return true
        }
        return false
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

